#!/bin/bash
#author merci
#default web dir :/data/www/ 

function add_vhost(){
	read -p "Please input your domain : " domain
	CONF="/usr/local/nginx/conf/vhost/$domain.conf"
	if [ ! -f "$CONF" ]
	then
		mkdir -p /data/www/$domain
		touch /data/www/$domain/index.php
		echo "<?php phpinfo(); ?>" > /data/www/$domain/index.php
		touch $CONF
		echo "server {\n	listen       80;\n	server_name www.$domain $domain;\n	index index.php index.html;\n 	root  /data/www/$domain;\n 	include php.conf;\n}" > $CONF
		/etc/init.d/nginx reload
		echo "Add vhost successfly! please visit http://$domain"
	else
		echo -e "Web config already exist !"
		exit 0
	fi
}
function del_vhost(){
	read -p "Please input the domain you want to delete : " domain
	CONF="/usr/local/nginx/conf/vhost/$domain.conf"
	if [ ! -f "$CONF" ]
	then
		echo -e "Web config not exist !"
		exit 0
	else
		rm -rf $CONF
		/etc/init.d/nginx reload
		echo -e "Web config deleted successfly !"
	fi
}
function list_vhost(){
	vhostlist=`ls /usr/local/nginx/conf/vhost/`
	for a in $vhostlist
	do
		echo $a
	done
}

echo -e "1.Add vhost config !"
echo -e "2.Del vhost config !"
echo -e "3.List vhost config !"
read -p "Please choose id :" c
case $c in
	1 )
		add_vhost
		;;
	2 )
		del_vhost
		;;
	3 )
		list_vhost
		;;
	* )
		echo "Please choose correct"
		;;
esac